package com.泛型;

import java.util.*;

@SuppressWarnings("all")
public class GenericExercise02 {
    public static void main(String[] args) {
        Dao<User> dao = new Dao<>();

        dao.save("jeak",new User("jeak",18,1));//添加
        dao.save("smith",new User("smith",28,2));//添加


        System.out.println(dao.get("jeak")); //获取jeak对应的value
        System.out.println(dao.get("smith"));

        System.out.println(dao.list());//获取整个value

        dao.update("jeak",new User("jeak2",18,3));//替换jeak对应的value
        System.out.println(dao.list());//获取整个value

        dao.delete("jeak");//删除jeak对应的对象
        System.out.println(dao.list());//获取整个value


    }
}

class Dao< T >{
    private Map<String , T> map = new HashMap<String , T>();

    public void save(String key , T value){
        map.put(key , value); //添加
    }

    public T get(String key){
        return map.get(key); //返回value
    }

    public void update(String key , T value){
        map.put(key , value); //替换
    }

    public List<T> list(){
        List<T> list = new ArrayList<>();

        Set<String> keyset = map.keySet();
        for (String key : keyset) {
            list.add(get(key));
        }
        return list;  //返回所以的value
    }

    public void delete(String key){
        map.remove(key); //删除
    }
}

class User{
    private String name;
    private int age;
    private int id;

    public User(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return name + "," + age + "," + id;
    }
}